package com.boarsoft.boar.soagov.face.biz;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boarsoft.boar.entity.FaceInfo;
import com.boarsoft.boar.soagov.face.FaceBiz;
import com.boarsoft.boar.soagov.face.FaceParamBiz;
import com.boarsoft.common.Util;
import com.boarsoft.common.dao.PagedResult;
import com.boarsoft.hibernate.biz.SimpleBizImpl;

@Component("faceBiz")
public class FaceBizImpl extends SimpleBizImpl implements FaceBiz {
	@Autowired
	FaceParamBiz faceParamBiz;

	@Override
	@Transactional(readOnly = true)
	public PagedResult<FaceInfo> list(String key, String orderBy, int pageNo, int pageSize) {
		StringBuilder sb = new StringBuilder();
		if (Util.strIsNotEmpty(key)) {
			sb.append("and (code like '%").append(key);
			sb.append("%' or name like '%").append(key);
			sb.append("%')");
		}
		int total = dao.getTotal(FaceInfo.class, sb.toString());
		if (Util.strIsNotEmpty(orderBy)) {
			// String ob = Util.array2str(("e." + orderBy).replaceAll(",",
			// ",").split(","), ",");
			sb.append(" order by ").append(orderBy);
		} else {
			sb.append(" order by name");
		}
		List<FaceInfo> lt = dao.list(FaceInfo.class, sb.toString(), pageNo, pageSize);
		return new PagedResult<FaceInfo>(total, lt, pageNo, pageSize);
	}

	@Override
	@Transactional
	public void save(FaceInfo o) {
		if (Util.strIsEmpty(o.getId())) {
			dao.save(o);
		} else {
			dao.merge(o);
		}
	}

	@Override
	@Transactional
	public void delete(String id) {
		dao.delete(FaceInfo.class, id);
	}

	@Override
	@Transactional(readOnly = true)
	public FaceInfo get(String id) {
		return (FaceInfo) dao.get(FaceInfo.class, id);
	}

	@Override
	@Transactional
	public String copy(String sourceId, String parentId, String newId) {
		FaceInfo o = this.get(sourceId);
		FaceInfo n = (FaceInfo) this.transfer(o, FaceInfo.class);
		n.setId(newId);// //id来自于前一步骤的dir_info表id
		dao.save(n);

		faceParamBiz.copy(sourceId, newId);
		return n.getId();
	}

	@Override
	@Transactional
	public boolean move(String id, String to) {
		// TODO 查找目标节点所属的库包，修改FaceInfo的libId?
		return true;
	}
}